package top.codedance.iotp.common.util;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;

public class VersionFileUtil {
    public static enum VersionName {
        Base(0), Alpha(1), Beta(2), Rc(3), Release(4);
        private int v;

        VersionName(int v){
            this.v = v;
        }

        public String toString(){
            switch (this.v){
                case 0:
                    return "Base";
                case 1:
                    return "Alpha";
                case 2:
                    return "Beta";
                case 3:
                    return "Rc";
                case 4:
                default:
                    return "Release";
            }
        }

        public static VersionName valueOf(int v){
            switch (v){
                case 0:
                    return Base;
                case 1:
                    return Alpha;
                case 2:
                    return Beta;
                case 3:
                    return Rc;
                case 4:
                default:
                    return Release;
            }
        }
    }
    private Class<?> clazz;
    private File versionFile;

    public VersionFileUtil(Class<?> clazz){
        this.clazz = clazz;
        String runtimeDirPath = SystemUtil.getRuntimeDirPath(this.clazz);
        String versionFilePath = runtimeDirPath + File.separator + "updateVersion";
        versionFile = new File(versionFilePath);
        if(!versionFile.exists()){
            try {
                versionFile.createNewFile();
                Field lastClientVersionCode = clazz.getField("lastClientVersionCode");
                long anLong = lastClientVersionCode.getLong(this.clazz);
                update(new Long(anLong).toString());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }else{
            try {
                update(get());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public void update(String versionCode) throws IOException, NoSuchFieldException, IllegalAccessException {
        OutputStream out = new FileOutputStream(versionFile);
        out.write(versionCode.getBytes(StandardCharsets.UTF_8));
        out.close();
        Field lastClientVersionCode = this.clazz.getField("lastClientVersionCode");
        lastClientVersionCode.setLong(this.clazz, Long.parseLong(versionCode));
    }

    public String get() throws IOException{
        String result = "0";
        InputStream in = new FileInputStream(versionFile);
        byte[] bs = new byte[(int)versionFile.length()];
        in.read(bs);
        in.close();
        result = new String(bs, StandardCharsets.UTF_8);
        result = result.replaceAll("\\n", "").replaceAll("\\r", "");
        return result;
    }
}
